package demo.liu.one.controller;

import demo.liu.one.configs.rabbitmq.ConfirmCallbackConfig;
import demo.liu.one.configs.rabbitmq.MqConfig;
import demo.liu.one.configs.rabbitmq.ReturnCallbackConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author by Liujt
 * @Classname TestMqController
 * @Description TODO
 * @Date 2021/6/21 17:39
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping("testMq")
public class TestMqController {

    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private ConfirmCallbackConfig confirmCallbackConfig;

    @Resource
    private ReturnCallbackConfig returnCallbackConfig;

    @GetMapping("direct")
    public void testDirect() {
        MessageProperties messageProperties = new MessageProperties();
        // 设置过期时间，单位：毫秒
        messageProperties.setExpiration("20000");
        byte[] msgBytes = "testDirect测试消息自动过期".getBytes();
        Message message = new Message(msgBytes, messageProperties);
        rabbitTemplate.convertAndSend(MqConfig.ITEM_DIRECT_QUEUE, message);
    }

    @GetMapping("noTopic")
    public void testNoTopic(@RequestParam("topic") String topic) {
        rabbitTemplate.setConfirmCallback(confirmCallbackConfig);
        rabbitTemplate.convertAndSend(topic, "item.springboot-rabbitmq", "testNoTopic");
    }

    @GetMapping("noQueue")
    public void testNoQueue(@RequestParam("queue") String queue) {
        rabbitTemplate.setReturnCallback(returnCallbackConfig);
        rabbitTemplate.convertAndSend(MqConfig.ITEM_TOPIC_EXCHANGE, queue, "testNoQueue");
    }

    @GetMapping("topic")
    public void testTopic() {
        rabbitTemplate.convertAndSend(MqConfig.ITEM_TOPIC_EXCHANGE, "item.springboot-rabbitmq", "testTopic");
    }


    @GetMapping("fanout")
    public void testFanout() {
        rabbitTemplate.convertAndSend(MqConfig.FANOUT_EXCHANGE, "", "testFanout");
    }


    @GetMapping("dead")
    public void testDead() {
        MessageProperties messageProperties = new MessageProperties();
        // 设置过期时间，单位：毫秒
        messageProperties.setExpiration("20000");
        byte[] msgBytes = "testDead测试消息自动过期".getBytes();
        Message message = new Message(msgBytes, messageProperties);
        rabbitTemplate.convertAndSend(MqConfig.COMMON_EXCHANGE, MqConfig.COMMON_ROUTING, message);
    }


    @GetMapping("rpc")
    public void testRpc() {
        MessageProperties messageProperties = new MessageProperties();
        // 设置过期时间，单位：毫秒
        messageProperties.setExpiration("20000");
        byte[] msgBytes = "testDead测试消息自动过期".getBytes();
        Message message = new Message(msgBytes, messageProperties);
        Object res = rabbitTemplate.convertSendAndReceive(MqConfig.COMMON_EXCHANGE, MqConfig.COMMON_ROUTING, message);
        log.info("RpcResMsg:{}", res);
    }

    @RabbitListener(queues = MqConfig.COMMON_QUEUE)
    public String testRpcRes(Message message) {
        log.info("Rpc接收：{}", new String(message.getBody()));
        return "Rpc返回";
    }

    /*@RabbitHandler(queues = MqConfig.COMMON_QUEUE)
    public String testRabbitHandler(Message message) {
        log.info("Rpc接收：{}", new String(message.getBody()));
        return "Rpc返回";
    }*/
}
